﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace StoreWeb.ajax
{
    public partial class userManage : System.Web.UI.Page
    {
        //BLL.Role rolebll = new BLL.Role();
        Maticsoft.BLL.UserAdmin Userbll = new Maticsoft.BLL.UserAdmin();
        BLL.DealersGrade dealersGradebll = new BLL.DealersGrade();
        Maticsoft.BLL.UserRoleRelation UserRolebll= new Maticsoft.BLL.UserRoleRelation();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //
                int row = 0;
                if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "delete")
                {
                    Response.Write((Userbll.Delete(Convert.ToInt32(Request["id"])) ? "1" : "0"));
                }
                else if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "suo")
                {

                    Maticsoft.Model.UserAdmin ne = Userbll.GetModel(Convert.ToInt32(Request["id"]));
                    ne.userstate = "已锁定";
                    row = Userbll.Update(ne) ? 2 : 0;
                    Response.Write(row);
                }
                else if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "jiesuo")
                {

                    Maticsoft.Model.UserAdmin ne = Userbll.GetModel(Convert.ToInt32(Request["id"]));
                    ne.userstate = "正常";
                    ne.logincount = 0;
                    row = Userbll.Update(ne) ? 2 : 0;
                    Response.Write(row);
                }
                else
                {
                    Maticsoft.Model.UserAdmin ne = new Maticsoft.Model.UserAdmin();
                    if (!string.IsNullOrEmpty(Request["id"]))
                    {
                        //username loginname   password mobile  email  ddlrole  description   dealers
                        ne = Userbll.GetModel(Convert.ToInt32(Request["id"]));
                        ne.username = Request["username"];
                        ne.loginname = Request["loginname"];
                        ne.password = Request["password"];
                        ne.mobile = Request["mobile"];
                        ne.email = Request["email"];


                        //ne.role = Request["role"];
     
                        ne.description = Request["description"];
                        ne.dealersid = Request["dealersid"];

                        row = Userbll.Update(ne) ? 2 : 0;

                        int urbid=Convert.ToInt32(UserRolebll.GetList(" userid=" + Request["id"] + " ").Tables[0].Rows[0][0]);
                        Maticsoft.Model.UserRoleRelation userrolerelation = UserRolebll.GetModel(urbid);
                        userrolerelation.roleid = Convert.ToInt32(Request["role"]);
                        UserRolebll.Update(userrolerelation);
                    }
                    else
                    {
                        ne.username = Request["username"];
                        ne.loginname = Request["loginname"];
                        ne.password = Request["password"];
                        ne.mobile = Request["mobile"];
                        ne.email = Request["email"];
                        ne.description = Request["description"];
                        ne.dealersid = Request["dealersid"];
                        ne.createtime = DateTime.Now;
                        ne.userstate = "正常";
                        int returnid = Userbll.Add(ne);
                        row = returnid > 0 ? 1 : 0;

                        Maticsoft.Model.UserRoleRelation urr = new Maticsoft.Model.UserRoleRelation();
                        urr.roleid = Convert.ToInt32(Request["role"]);
                        urr.userid = returnid;
                        new Maticsoft.BLL.UserRoleRelation().Add(urr);
                    }
                    if (row == 1)
                    {
                        Response.Write("1");
                    }else if (row == 2)
                    {
                        Response.Write("2");
                    }
                    else
                    {
                        Response.Write("0");
                    }
                }
            }
        }
    }
}